﻿using AutoMapper;
using MediatR;
using S.Produce.Api.Application.Commands.ShiftCommands;
using S.Produce.Domains;
using S.Produce.Infrastructure;

namespace S.Produce.Api.Application.CommandHandlers.ShiftCommandHandlers
{
    public class ShiftUpdateCommandHandler : IRequestHandler<ShiftUpdateCommand, int>
    {
        private readonly IRepository<ShiftDorm> shiftdorm;
        private readonly IMapper mapper;

        public ShiftUpdateCommandHandler(IRepository<ShiftDorm> shiftdorm, IMapper mapper)
        {
            this.shiftdorm = shiftdorm;
            this.mapper = mapper;
        }

        public async Task<int> Handle(ShiftUpdateCommand request, CancellationToken cancellationToken)
        {
            var info = await shiftdorm.GetAsync(request.SDId);
            request.UpdateBy = "李四";
            request.UpdateDate = DateTime.Parse(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
            return await shiftdorm.UpdateAsync(mapper.Map(request, info));
        }
    }
}
